package models;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import play.data.validation.Required;
import play.db.jpa.Model;

@Entity
@Table(name = "Roles")
public class Role extends Model {

	@Required
	public String title;

	public boolean isSystem;

	@ManyToMany
	//@JoinTable(name = "Actions_Roles", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "action_id"),uniqueConstraints = { @UniqueConstraint(columnNames = {
	//		"role_id", "action_id"}) })
	@JoinTable(name = "Actions_Roles", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "action_id"))
	public List<Action> actions = new LinkedList<Action>();

	@OneToMany(mappedBy = "role", cascade = { CascadeType.REMOVE })
	public List<UserRoleProject> user_project_roles = new LinkedList<UserRoleProject>();

	/**
	 * contructs a new role
	 * 
	 * @param title
	 * @param isSystem
	 */
	public Role(String title, boolean isSystem) {
		super();
		this.title = title;
		this.isSystem = isSystem;
	}

	/**
	 * constructs a new role
	 * 
	 * @param title
	 */
	public Role(String title) {
		super();
		this.title = title;
	}

	/**
	 * Defines the String representation of the Role
	 * 
	 */
	public String toString() {
		return title;
	}
}
